﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKStairs.HYElevator.Manage
{
	public class ElevatorXDataManager
	{
		private static Schema CreateSimpleSchema()
		{
			SchemaBuilder schemaBuilder = new SchemaBuilder(ElevatorXDataManager.m_Guid);
			schemaBuilder.SetReadAccessLevel((AccessLevel)1);
			schemaBuilder.SetSchemaName(ElevatorXDataManager.m_sElevatorCommonent);
			schemaBuilder.AddSimpleField(ElevatorXDataManager.m_sElevatorStopLayer, typeof(string));
			schemaBuilder.AddSimpleField(ElevatorXDataManager.m_sElevatorStopNum, typeof(int));
			FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField(ElevatorXDataManager.m_sElevatorLiftHeight, typeof(double));
			fieldBuilder.SetUnitType(0);
			schemaBuilder.AddArrayField(ElevatorXDataManager.m_sLegendId, typeof(ElementId));
			fieldBuilder.SetUnitType(0);
			return schemaBuilder.Finish();
		}

		private static bool HasSchema(out Schema schema)
		{
			Schema schema2 = Schema.Lookup(ElevatorXDataManager.m_Guid);
			schema = schema2;
			return schema2 != null;
		}

		public static void WriteElevatorComment(Element element, string strStopLayer, int nStopNum, double dLiftHeight, List<ElementId> lst2dLegend = null)
		{
			Schema schema = null;
			if (!ElevatorXDataManager.HasSchema(out schema))
			{
				schema = ElevatorXDataManager.CreateSimpleSchema();
			}
			if (ElevatorXDataManager.m_ElevatorCommontEnt == null)
			{
				ElevatorXDataManager.m_ElevatorCommontEnt = new Entity(schema);
			}
			Field field = schema.GetField(ElevatorXDataManager.m_sElevatorStopLayer);
            ElevatorXDataManager.m_ElevatorCommontEnt.Set<string>(field, strStopLayer, (DisplayUnitType)(-2));
			field = schema.GetField(ElevatorXDataManager.m_sElevatorStopNum);
            ElevatorXDataManager.m_ElevatorCommontEnt.Set<int>(field, nStopNum, (DisplayUnitType)(-2));
			field = schema.GetField(ElevatorXDataManager.m_sElevatorLiftHeight);
			ElevatorXDataManager.m_ElevatorCommontEnt.Set<double>(field, dLiftHeight, (Autodesk.Revit.DB.DisplayUnitType)6);
			field = schema.GetField(ElevatorXDataManager.m_sLegendId);
			if (lst2dLegend != null)
			{
				ElevatorXDataManager.m_ElevatorCommontEnt.Set<IList<ElementId>>(field, lst2dLegend);
			}
			else
			{
				ElevatorXDataManager.m_ElevatorCommontEnt.Set<IList<ElementId>>(field, new List<ElementId>());
			}
			element.SetEntity(ElevatorXDataManager.m_ElevatorCommontEnt);
		}

		public static bool ReadElevatorCommont(Element element, ref string strStopLayers, ref int nStopNum, ref double dLiftHeight, ref List<ElementId> lst2DLegend)
		{
			Schema schema = null;
			if (ElevatorXDataManager.HasSchema(out schema))
			{
				Entity entity = element.GetEntity(schema);
				if (entity.IsValid())
				{
					Field field = schema.GetField(ElevatorXDataManager.m_sElevatorStopLayer);
					if (field != null)
					{
						strStopLayers = entity.Get<string>(field);
					}
					field = schema.GetField(ElevatorXDataManager.m_sElevatorStopNum);
					if (field != null)
					{
						nStopNum = entity.Get<int>(field);
					}
					field = schema.GetField(ElevatorXDataManager.m_sElevatorLiftHeight);
					if (field != null)
					{
						dLiftHeight = entity.Get<double>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
					}
					field = schema.GetField(ElevatorXDataManager.m_sLegendId);
					if (field != null)
					{
						lst2DLegend = (entity.Get<IList<ElementId>>(field) as List<ElementId>);
					}
					return true;
				}
			}
			return false;
		}

		public static Guid m_Guid = new Guid("294FF531-A749-4265-9A27-56607C8B13F2");

		public static string m_sElevatorCommonent = "ElevatorComment";

		public static string m_sElevatorStopLayer = "ElevatorStopLayer";

		public static string m_sElevatorStopNum = "ElevatorStopNum";

		public static string m_sElevatorLiftHeight = "ElevatorLiftHeight";

		public static string m_sLegendId = "LegendId";

		public static Entity m_ElevatorCommontEnt = null;
	}
}
